import fractions
import random
from random import randint

def _probability(denominator):
    return random.random() < (1 / float(denominator))


def equationsWithFractions():    
    """
    1:08 q1
    """
    if _probability(5):
        return getFraction()
    else:
        return getNormal()

    
def getFraction():
    pronumeral = randint(1, 15)#10
    constant = randint(-20, 20)#15
    denominator = randint(2, 6)#5
    
    result = fractions.Fraction(pronumeral, denominator) + constant
            
    if constant == 0:
        problemString  = '\\frac{x}{' + str(denominator) + '} = ' + ('\\frac{%s}{%s}'%(result.numerator,result.denominator))
    elif constant < 0:
        problemString = '\\frac{x}{' + str(denominator) + "} - " + str(abs(constant)) + " = " + ('\\frac{%s}{%s}'%(result.numerator,result.denominator))

    answer = (result - constant) / result.denominator

    return problemString, answer


def getNormal():
    pronumeral = randint(1, 15)#10
    constant = randint(-20, 20)#15
    coefficient = randint(1, 12)

    result = coefficient * pronumeral + constant

    if constant == 0:  strConstant = ''
    elif constant < 0: strConstant = '- ' + str(abs(constant))
    else:              strConstant = '+ ' + str(constant)

    if coefficient == 1: strCoef = 'x'
    else:                strCoef = str(coefficient) + 'x'

    problemString = strCoef + ' ' + strConstant + ' = ' + str(result)
    
    return problemString, pronumeral

#equationsWithFractions()
